/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.mall.rule.ReturnProduct
   Module Description   :

   Date Created      : 2009/12/30
   Original Author   : andy.chuang
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.ec.order.rule;

import java.math.BigDecimal;
import java.math.RoundingMode;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author andy.chuang
 * 
 */
public class ReturnProductRule {
	private static Logger LOGGER = LoggerFactory.getLogger(ReturnProductRule.class);

	public static BigDecimal calculateReturnAmount(BigDecimal totalAmount, BigDecimal coupon, BigDecimal totalDiscount,
			BigDecimal giftcard) {
		LOGGER.debug(totalAmount + ".subtract(" + coupon + ")=" + totalAmount.subtract(coupon));
		// 禮品卡獨立運算 所以這裡要減掉
		return totalAmount.subtract(coupon).subtract(totalDiscount).subtract(giftcard);
	}

	public static void main(String args[]) {
		// 1
		// Return ret=calculateReturn(new BigDecimal(150), new BigDecimal(50), new BigDecimal(50), new BigDecimal(0), new
		// BigDecimal(50), null, new BigDecimal(25));
		// System.out.println(ret.getBonus());
		// System.out.println(ret.getMyWallet());
		// System.out.println(ret.getCash());

	}

	public static Return calculateReturn(BigDecimal orderBonus, BigDecimal orderMyWallet, BigDecimal orderBenefit,
			BigDecimal orderCash, BigDecimal orderShipfare, BigDecimal returnAmount // 可退总金额
			, BigDecimal totalInstallmentFee, BigDecimal orderGiftcard) {
		Return ret = new Return();
		ret.setBenefit(new BigDecimal(0));
		ret.setBonus(new BigDecimal(0));
		ret.setCash(new BigDecimal(0));
		ret.setMyWallet(new BigDecimal(0));
		ret.setShipfare(new BigDecimal(0));
		LOGGER.debug(" orderBonus, orderMyWallet, orderBenefit, orderCash, orderShipFare, returnAmount,orderGiftcard:"
				+ orderBonus + "," + orderMyWallet + "," + orderBenefit + "," + orderCash + "," + orderShipfare + ","
				+ returnAmount + "," + orderGiftcard);
		if (orderBonus == null) {
			orderBonus = BigDecimal.ZERO;
		}
		if (orderCash == null) {
			orderCash = BigDecimal.ZERO;
		}
		if (orderMyWallet == null) {
			orderMyWallet = BigDecimal.ZERO;
		}
		if (totalInstallmentFee == null) {
			totalInstallmentFee = BigDecimal.ZERO;
		}
		if (orderGiftcard == null) {
			orderGiftcard = BigDecimal.ZERO;
		}
		BigDecimal theSumAmount = orderBonus.add(orderCash).add(orderMyWallet).add(orderGiftcard);
		Float otp = theSumAmount.floatValue();
		Float ra = returnAmount.floatValue();
		if (((orderMyWallet != null && orderMyWallet.floatValue() != 0) || (orderGiftcard != null && orderGiftcard
				.floatValue() != 0))
				&& (orderCash != null || orderCash.floatValue() != 0)) {

			if (orderBonus != null) {
				Float ob = orderBonus.floatValue();
				// Float otp = orderTotalAmount.floatValue();
				Float bonus = ob / otp * ra;
				ret.setBonus(new BigDecimal(bonus).setScale(0, RoundingMode.UP));// 积分折抵采无条件进位
			}

			if (orderCash != null) {
				// Float otp = orderTotalAmount.floatValue();
				Float oc = orderCash.floatValue();
				Float cash = oc / otp * ra;
				Integer cashI = Math.round(cash);
				ret.setCash(new BigDecimal(cashI));
			}
			if (orderMyWallet != null) {
				Float myWallet = ra - ret.getBonus().intValue() - ret.getCash().intValue();
				ret.setMyWallet(new BigDecimal(myWallet));

			}

		} else if ((orderMyWallet == null || orderMyWallet.floatValue() == 0) && orderCash != null
				&& orderCash.floatValue() != 0) {
			if (orderBonus != null) {
				Float ob = orderBonus.floatValue();
				Float bonus = ob / otp * ra;
				ret.setBonus(new BigDecimal(bonus).setScale(0, RoundingMode.UP));// 积分折抵采无条件进位

			}
			if (orderCash != null) {
				Float cash = ra - ret.getBonus().intValue();

				ret.setCash(new BigDecimal(cash));

			}

		} else if ((orderMyWallet.floatValue() != 0 && orderMyWallet != null)
				&& (orderCash == null || orderCash.floatValue() == 0)) {
			if (orderBonus != null) {
				Float ob = orderBonus.floatValue();
				Float bonus = ob / otp * ra;
				ret.setBonus(new BigDecimal(bonus).setScale(0, RoundingMode.UP));// 积分折抵采无条件进位

			}
			if (orderMyWallet != null) {
				Float myWallet = ra - ret.getBonus().intValue();

				ret.setMyWallet(new BigDecimal(myWallet));

			}

		}
		// 有分期手續費時才需計算，否則會造成被除數為0的狀況
		if (totalInstallmentFee.floatValue() > 0) {
			ret.setInstallmentFee(new BigDecimal(ra / (otp - orderShipfare.floatValue() - totalInstallmentFee.floatValue())
					* totalInstallmentFee.floatValue()).setScale(1, RoundingMode.DOWN));
		} else {
			ret.setInstallmentFee(BigDecimal.ZERO);
		}

		LOGGER.debug("return:{}" + ret);
		return ret;
	}
}
